



/* Compiler options ---------------------------------------------------------*/
//sdcc -c -mmcs51 --std-sdcc99 --verbose --model-large


/* Linker options -----------------------------------------------------------*/
//sdcc --code-loc 0x0000 --code-size 0x8000 --xram-loc 0x0000 --xram-size 0x8000 --model-large --out-fmt-ihx

/* Macros -------------------------------------------------------------------*/ 
#define HEAP_SIZE 0x708   // Set heap size to 1800 bytes options ------------*/

const unsigned char COORDINATOR[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const unsigned char ROUTER1[]={0x00,0x13,0xA2,0x00,0x40,0x81,0x39,0x85};
const unsigned char ROUTER2[]={0x00,0x13,0xA2,0x00,0x40,0x61,0xAC,0x80};



/* Includes -----------------------------------------------------------------*/
#include "main.h"
#include "xbee.h"
#include "bbserial.h"

/* Externalized Variables ---------------------------------------------------*/
unsigned char xdata heap[HEAP_SIZE];	//Allocate heap in external memory
unsigned char * send;

/* Global Variables ---------------------------------------------------------*/



/* ----------------------------------------------------------------------------
 * _sdcc_external_startup() - initializes serial port, baud rate, and internal RAM
 * ---------------------------------------------------------------------------- */
_sdcc_external_startup()
{
	SCON=0x52;		//Serial port Mode 8-bit UART and set TI flag
	BDRCON&=0xEE;	
	BDRCON|=0x1E;	
	BRL=0xFD;		//SET BAUD RATE TO 57600
//	BRL=0xED;		//SET BAUD RATE TO 9600
	AUXR=0xC;
	
	//Set priority of Serial Interrupts to be higher than T0
	IPH0=0x10;
	
	/*Enable all interrupts, Timer 0 overflow enabled*/
	IEN0=0x86;
	
	/*Set Timer 0 to overflow every 10ms*/
	TH0=0x4c;
	TL0=0;
	
	P1_1=1;
	return 0;
}


/*---------------------------------------------------------------------------*/
void main(){
	unsigned int i=0;
	unsigned char c='0';
//	unsigned char command[]={0x7E,0x00,0x0A,0x01,0x01,0x00,0x00,0x00,0x48,0x65,0x6C,0x6C,0x6F,0xB8};
//	
	/*
	 * 7E Start delimiter
	 * 00 0A Length bytes
	 * 01 API identifier
	 * 01 API frame ID
	 * 50 01 Destination address low
	 * 00 Option byte
	 * 48 65 6C 6C 6F Data packet
	 * B8 Checksum
	*/
	
	
/*----------------------------------------------------------------------------
 * Functions to initialize:
 *  External Mmeory, LCD, EEPROM, Timer, and I/O expander
 * --------------------------------------------------------------------------*/
//	init_dynamic_memory((MEMHEADER xdata *)heap, HEAP_SIZE); //initializes heap
//
//
//	putstr(command);
//		


//	send=remote_AT_command_request(&ROUTER2,0xFFFE);
//	
//	for(i=0;i<20;i++){
//		printf("%02X",send[i]);
//	}

	send=COORDINATOR;

	for(i=0;i<8;i++){
		printf("%x",(send[i]));
	}
	putstr("\n\r\n\r");

	send=ROUTER1;
	for(i=0;i<8;i++){
		printf("%x",(send[i]));
	}
	send=ROUTER2;
	for(i=0;i<8;i++){
		printf("%x",(send[i]));
	}



// Toggle the LED at P1.0 when code has finished executing.
	while(1){
		c=bbgetchar();
		bbputchar(c);
	}
}


/* ----------------------------------------------------------------------------
 * putstr()
 * --------------------------------------------------------------------------*/

int putstr (char *s)
{
	int i = 0;
	while (*s){			// output characters until NULL found
		putchar(*s++);
		i++;
		/*use counter i to keep track of number of characters printed per line 
		 * to keep the output from going over 60 characters per line */
		if(*s=='\n'||*s=='\r'){
			i=0;
		}
		if(i==60)
		{
			putchar('\n');
			putchar('\r');
			i=0;
		}
	}
	return i+1;
}

/* ----------------------------------------------------------------------------
 * putchar()
 * ----------------------------------------------------------------------------
 * Outputs a character to the output.
 * --------------------------------------------------------------------------*/

void putchar (char c)
{
	while (!TI){P1_0=0;}
	SBUF = c;	// load serial port with transmit value
	TI = 0;  	// clear TI flag
	P1_0=1;
}

/* ----------------------------------------------------------------------------
 * getchar()
 * ----------------------------------------------------------------------------
 * Gets inputs and returns it
 * --------------------------------------------------------------------------*/

char getchar ()
{
    while (!RI);
	RI = 0;			// clear RI flag
	return SBUF;  	// return character from SBUF
}

/* --------------------------------------------------------------------------*/
/* 1.1 ms delay function for 8051 -------------------------------------------*/
/* --------------------------------------------------------------------------*/
void delay_ms(long ms)
{
    long i;
	/* loop below takes approximately 1.1ms, tested on oscilloscope */
    while (ms--){
        for (i=0; i < 100; i++){
        }
    }
}
